const express = require('express')
const router = express.Router()



router.get('/', (req, res) => {
    res.send('hello')
})
//首页-业务指标数据和同比增长率
router.get('/api/dashboard/summary', (req, res) => {
    // 基础数据模板
    const baseData = {
        orderCount: 1200,       // 订单基数
        refundCount: 70,        // 退款基数
        newUserCount: 300,      // 新增用户基数
        salesAmount: 85000,     // 销售总额基数（元）
        activeUserCount: 5500   // 活跃用户基数
    };

    // 生成随机波动（±5%）
    function getRandomFluctuation(baseValue, minFactor = 10, maxFactor = 80) {
        const factor = minFactor + Math.random() * (maxFactor - minFactor);
        return Math.floor(baseValue * factor);
    }

    // 计算增长率（百度分）
    function calculateGrowthRate(today, yesterday) {
        return ((today - yesterday) / yesterday * 100).toFixed(2);
    }

    // 生成模拟数据
    function generateDashboardData() {
        // 今日数据（带随机波动）
        const today = {
            orderCount: getRandomFluctuation(baseData.orderCount),
            refundCount: getRandomFluctuation(baseData.refundCount),
            newUserCount: getRandomFluctuation(baseData.newUserCount),
            salesAmount: getRandomFluctuation(baseData.salesAmount, 0.9, 1.1).toFixed(2), // 销售波动更大
            activeUserCount: getRandomFluctuation(baseData.activeUserCount)
        };

        // 昨日数据（比今日稍低，带随机波动）
        const yesterday = {
            orderCount: getRandomFluctuation(today.orderCount, 0.9, 0.98),
            refundCount: getRandomFluctuation(today.refundCount, 0.9, 0.98),
            newUserCount: getRandomFluctuation(today.newUserCount, 0.9, 0.98),
            salesAmount: getRandomFluctuation(parseFloat(today.salesAmount), 0.9, 0.98).toFixed(2),
            activeUserCount: getRandomFluctuation(today.activeUserCount, 0.9, 0.98)
        };

        // 计算增长率
        const growthRate = {
            orderCount: calculateGrowthRate(today.orderCount, yesterday.orderCount),
            refundCount: calculateGrowthRate(today.refundCount, yesterday.refundCount),
            newUserCount: calculateGrowthRate(today.newUserCount, yesterday.newUserCount),
            salesAmount: calculateGrowthRate(parseFloat(today.salesAmount), parseFloat(yesterday.salesAmount)),
            activeUserCount: calculateGrowthRate(today.activeUserCount, yesterday.activeUserCount)
        };

        return {
            code: 200,
            message: 'success',
            data: {
                today,
                yesterday,
                growthRate
            }
        }
    }
     const dashboardData = generateDashboardData();
    res.send(dashboardData);
})



module.exports = router